/**
* COPYRIGHT. whx. ALL RIGHTS RESERVED.
* Project: just-wenda-web
* Author: whx <harrywu304@gmail.com>
* Create On: Mar 1, 2012 9:38:17 AM
*/

package just.sample.wenda.dao;

import java.util.Map;

import just.sample.wenda.model.Topic;

import org.justframework.api.connector.ClientConnector;
import org.justframework.api.connector.ClientConnectorFactory;
import org.justframework.api.connector.Request;
import org.justframework.api.connector.Response;

/**
 * Topic Dao
 */
public class TopicDAO {
	
	private static ClientConnector client = ClientConnectorFactory.getClientConnector("vm");
	
	/**
	 * create topic
	 * @param params
	 * @return
	 */
	public static String createTopic(Map params){
		Request req = new Request();
		req.setUriString("vm://localhost:8080/topic");
		req.setRequestMethod("post");
		req.setParams(params);
		Response res = client.rpc(req);
		System.out.println(res);
		return res.getContent(String.class);
	}
	
	/**
	 * delete topic
	 * @param topicId
	 * @return
	 */
	public static boolean deleteTopic(String topicId){
		Request req = new Request();
		req.setUriString("vm://localhost:8080/topic");
		req.setRequestMethod("delete");
		req.setParam("topicId", topicId);
		Response res = client.rpc(req);
		return res.getContent(Boolean.class);		
	}
	
	/**
	 * query topic
	 * @param topicId
	 * @return
	 */
	public static Topic getTopic(String topicId){
		Request req = new Request();
		req.setUriString("vm://localhost:8080/topic");
		req.setRequestMethod("get");
		req.setParam("topicId", topicId);
		Response res = client.rpc(req);
		return res.getContent(Topic.class);
	}
	
	/**
	 * update topic
	 * @param params
	 * @return
	 */
	public static boolean updateTopic(Map params){
		Request req = new Request();
		req.setUriString("vm://localhost:8080/topic");
		req.setRequestMethod("put");
		req.setParams(params);
		Response res = client.rpc(req);
		System.out.println(res);
		return res.getContent(Boolean.class);
	}

}
